package com.yunhe.log.service.base.impl;

import com.yunhe.common.model.PageParam;
import com.yunhe.log.domain.base.SystemMonitorLogType;
import com.yunhe.log.repository.base.SystemMonitorLogTypeRepository;
import com.yunhe.log.service.base.SystemMonitorLogTypeService;
import com.yunhe.common.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统监控日志类型Service
 * @desc 系统监控日志类型服务类实现
 * @author liuronglei
 */
@Service
public class SystemMonitorLogTypeServiceImpl implements SystemMonitorLogTypeService {
    
    @Autowired
    private SystemMonitorLogTypeRepository systemMonitorLogTypeRepository;
    
//    private void processBeforeSave(SystemMonitorLogType systemMonitorLogType) {
//
//    }
//
//    /**
//     * 新增系统监控日志类型
//     * @param systemMonitorLogType 新增对象
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public SystemMonitorLogType createSystemMonitorLogType(SystemMonitorLogType systemMonitorLogType)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        systemMonitorLogType.setId(null);
//        processBeforeSave(systemMonitorLogType);
//        return systemMonitorLogTypeRepository.save(systemMonitorLogType);
//    }
//
//    /**
//     * 批量新增系统监控日志类型
//     * @param list 系统监控日志类型列表
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public Iterable<SystemMonitorLogType> createSystemMonitorLogType(List<SystemMonitorLogType> list)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        for (SystemMonitorLogType SystemMonitorLogType : list) {
//            SystemMonitorLogType.setId(null);
//            processBeforeSave(SystemMonitorLogType);
//        }
//        return systemMonitorLogTypeRepository.saveAll(list);
//    }
//
//    /**
//     * 根据id删除系统监控日志类型
//     * @param id id
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public void delete(Long id) {
//        systemMonitorLogTypeRepository.deleteById(id);
//    }
//
//    /**
//     * 更新系统监控日志类型（更新全部字段）
//     * @param systemMonitorLogType 系统监控日志类型对象
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public SystemMonitorLogType updateSystemMonitorLogType(SystemMonitorLogType systemMonitorLogType)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        boolean exists = systemMonitorLogType.getId() != null && systemMonitorLogTypeRepository.existsById(systemMonitorLogType.getId());
//        if (!exists) {
//            throw new ObjectNotFoundException("找不到ID为" + systemMonitorLogType.getId() + "的系统监控日志类型");
//        }
//        processBeforeSave(systemMonitorLogType);
//        return systemMonitorLogTypeRepository.save(systemMonitorLogType);
//    }
//
//    /**
//     * 更新系统监控日志类型（更新指定字段）
//     * @param id id
//     * @param code 系统监控日志类型代码
//     * @param name 系统监控日志类型编号
//     * @param title 系统监控日志类型标题
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public SystemMonitorLogType updateSystemMonitorLogType(Long id,
//                                                           Optional<Long> code,
//                                                           Optional<String> name,
//                                                           Optional<String> title,
//                                                           Optional<Long> sn,
//                                                           Optional<Boolean> activity)
//            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
//        SystemMonitorLogType systemMonitorLogType = this.findById(id);
//        if (systemMonitorLogType == null) {
//            throw new ObjectNotFoundException("找不到ID为" + id + "的系统监控日志类型");
//        }
//        code.ifPresent(systemMonitorLogType::setCode);
//        name.ifPresent(systemMonitorLogType::setName);
//        title.ifPresent(systemMonitorLogType::setTitle);
//        sn.ifPresent(systemMonitorLogType::setSn);
//        activity.ifPresent(systemMonitorLogType::setActivity);
//        processBeforeSave(systemMonitorLogType);
//        return systemMonitorLogTypeRepository.save(systemMonitorLogType);
//    }

    /**
     * 根据ID获得系统监控日志类型对象
     * @param id id
     */
    @Override
    public SystemMonitorLogType findById(Long id) {
        return systemMonitorLogTypeRepository.findById(id).orElse(null);
    }

    @Override
    public SystemMonitorLogType findByName(String name) {
        return systemMonitorLogTypeRepository.findByName(name);
    }

    /**
     * 获得所有系统监控日志类型对象
     * @param pageParam 分页对象
     */
    @Override
    public Iterable<SystemMonitorLogType> findSystemMonitorLogTypes(PageParam pageParam) {
        if (pageParam.getSort() == null) {
            List<String> list = new ArrayList<>();
            list.add("sn");
            pageParam.setSort(list);
        }
        Iterable<SystemMonitorLogType> results;
        if (QueryUtil.needPaging(pageParam)) {
            results = systemMonitorLogTypeRepository.findAll(QueryUtil.getPageRequest(pageParam));
        } else {
            results = systemMonitorLogTypeRepository.findAll(QueryUtil.getSort(pageParam.getSort()));
        }
        if (results == null) {
            results = new ArrayList<>();
        }
        return results;
    }
}
